<?php
echo $this->renderPartial("leftnav"); 
?>
<div class="grid_712">
    <div class="box">
        <h1>Giỏ Hàng Của Tôi</h1>

        <table class="tb_general">
            <tr>
                <th>STT          </th>
                <th>Tên Sản Phẩm </th>
                <th>Số Lượng     </th>
                <th>Giá Tiền     </th>
                <th>Thành Tiền   </th>
                <th>Xóa          </th>
            </tr>

            <?php foreach ($ss_cart as $index=>$o_productCart) {?>
            <tr id="tr_<?=$index;?>">
                <td><?=$o_productCart["key"];?>    </td>
                <td><?=$o_productCart["subject"]?> </td>

                <td>
                    <input
                        id         = "txt_amout<?=$index;?>"
                        name       = "txt_amout<?=$index;?>"
                        class      = "text_box"
                        value      = "<?=$o_productCart["amount"];?>"
                        style      = "width: 52px;"
                        onblur     = "cart_update(<?=$index;?>, this.value);"
                        onkeypress = "return numeric_press(event);"
                    />
                </td>

                <td><?=number_format($o_productCart["price"],0);?> VNĐ</td>

                <td id="td_amount<?=$index;?>"><?=number_format($o_productCart["price"] * $o_productCart["amount"],0);?> VNĐ</td>

                <td>
                    <span
                        class   = "icon-cancel-circled icon_button"
                        onclick = "cart_delete(<?=$index;?>)"
                    ></span>
                </td>
            </tr>
            <?php }//End foreach?>

            <tr>
                <td colspan="7" class="td_button" style="text-align: right;">
                    <input
                        id    = "btn_return"
                        name  = "btn_return"
                        type  = "button"
                        class = "button_css3"
                        value = "Tiếp Tục Mua Hàng"
                    />

                    <input
                        id    = "btn_cartDeleteAll"
                        name  = "btn_cartDeleteAll"
                        type  = "button"
                        class = "button_css3"
                        value = "Hủy Giỏ Hàng"
                    />
                </td>
            </tr>
        </table>

        <div class="box_inside">
            <div class="header_full">Hình Thức Thanh Toán</div>

            <div class="galleries_item">
                <div class="item_vertical">
                    <a href="<?=Yii::app()->createAbsoluteUrl("cart/checkout");?>">
                        <img alt="Gọi Đặt Hàng" src="<?=Yii::app()->request->baseUrl;?>/images/image_default/order_product.png">
                        <span class="item_title">Đặt Hàng</span><br/>
                    </a>
                </div>
            </div><!-- End galleries_item -->
        </div><!-- End box_inside -->
    </div><!-- End box -->
</div><!-- End grid_712 -->
    
<script type="text/javascript">

$('#btn_return').click(function()
{
    load_page("");
});

$('#btn_cartDeleteAll').click(function()
{
    if( !confirm("Bạn muốn hủy giỏ hàng?") )
    {
        return;
    }//End if

    $.ajax({
        url         : "<?=Yii::app()->createAbsoluteUrl("cart/deleteCart");?>",
        type        : "POST",
        cache       : false,
        data        : {item_key : -1},
        contentType : "application/x-www-form-urlencoded; charset=UTF-8",
        dataType    : "text",
        success     : function(response)
        {
            var arr_result = $.parseJSON(response);

            if(arr_result.error != null)
            {
                alert(arr_result.error);
                return;
            }

            if(arr_result.result != null && arr_result.result == true)
                load_page("");
        },
        error: function ()
        {
            alert("Có lỗi xảy ra.");
        }
    });//End Ajax
});//End btn_cartDeleteAll.click


function cart_delete(i_key)
{
    if ( !confirm("Bạn muốn xóa các đối tượng đã chọn?") )
    {
        return;
    }

    $.ajax({
        url         : "<?=Yii::app()->createAbsoluteUrl("cart/delete");?>",
        type        : "POST",
        cache       : false,
        data        : {item_key: i_key},
        contentType : "application/x-www-form-urlencoded; charset=UTF-8",
        dataType    : "html",
        success     : function(result)
        {
        	var arr_result = $.parseJSON(result);

            if(arr_result.error != null)
            {
                alert(arr_result.error);
                return;
            }

            if( arr_result.result != null && arr_result.result == true )
            {
                $("#tr_"+i_key).remove();
                $("#btn_cart_url").text("có"+ result +" sản phẩm");
                                    
            };
        },//End success
        error : function ()
        {
            alert("Có lỗi xảy ra.");
        }
    });//End Ajax
}//End cart_delete


function cart_update(i_key, i_quantity)
{
    if( i_quantity >= 50 )
    {
        alert("Số lượng bạn nhập không được quá 50 sản phẩm");
        $("#txt_amout"+i_key).focus();
        return;
    }

    $.ajax({
        url   : "<?=Yii::app()->createAbsoluteUrl("cart/update");?>",
        type  : "POST",
        cache : false,
        data  : {
        	item_key     : i_key,
            txt_quantity : i_quantity
        },
        contentType : "application/x-www-form-urlencoded; charset=UTF-8",
        dataType    : "html",
        success     : function(result)
        {
            var arr_result = $.parseJSON(result);

            if(arr_result.error != null)
            {
                alert(arr_result.error);
                return;
            }

            if ( arr_result.result >= 0 )
            {
                $("#td_amount"+i_key).text(format_currency(arr_result.result) + " VNĐ");
            }
            else
            {
                alert("Có lỗi xảy ra");
                $("#txt_amout"+i_key).focus();
            }
        },//End success
        error: function (){
            alert("Có lỗi xảy ra.");
        }
    });//End Ajax
}//End cart_update
</script>